 var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;

		form.verify({
            title : function(value) {
            	
            	 if(value == ''){
            		 return '登录名不能为空';
            	 }
            	
            	 if (value.length  > 25) {
                     return '不能输入大于25个字符';
                 } 
            	
                var usersData ;
            	$.ajax({
                    type : "get",
                    url : "/user/existLoginName",
                    async:false,
                    dataType : "json",
                	data:{
            			'loginName':value
            		},
                    success : function(data) {
                    	usersData = data;
                    }
                });
                
            	 if (usersData == 1) {
                      return '登录名已经存在';
                  }   
            	
            },
            number : [/^[0-9]*$/, '必须输入数字啊']
        });
		
 	form.on("submit(addUser)",function(data){
 		$.ajax({
            type : "post",
            url : "/user/add",
            async:false,
        	dataType: "json",
    		data: $("form").serialize(),
            success : function(res) {
                  console.log(res);
         			top.layer.msg("用户添加成功！");
         			top.layer.closeAll("iframe");
         	 		//刷新父页面
         	 	 	parent.location.reload();
            }
        });
 		
 		
    
 		
 		
 		
 		
 		
 		
 		
 		//是否添加过信息
	/* 	if(window.sessionStorage.getItem("addUser")){
	 		addUserArray = JSON.parse(window.sessionStorage.getItem("addUser"));
	 	}

	 	var userStatus,userGrade,userEndTime;
	 	//会员等级
	 	if(data.field.userGrade == '0'){
 			userGrade = "注册会员";
 		}else if(data.field.userGrade == '1'){
 			userGrade = "中级会员";
 		}else if(data.field.userGrade == '2'){
 			userGrade = "高级会员";
 		}else if(data.field.userGrade == '3'){
 			userGrade = "超级会员";
 		}
 		//会员状态
 		if(data.field.userStatus == '0'){
 			userStatus = "正常使用";
 		}else if(data.field.userStatus == '1'){
 			userStatus = "限制用户";
 		}

 		addUser = '{"usersId":"'+ new Date().getTime() +'",';//id
 		addUser += '"userName":"'+ $(".userName").val() +'",';  //登录名
 		addUser += '"userEmail":"'+ $(".userEmail").val() +'",';	 //邮箱
 		addUser += '"userSex":"'+ data.field.sex +'",'; //性别
 		addUser += '"userStatus":"'+ userStatus +'",'; //会员等级
 		addUser += '"userGrade":"'+ userGrade +'",'; //会员状态
 		addUser += '"userEndTime":"'+ formatTime(new Date()) +'"}';  //登录时间
 		console.log(addUser);
 		addUserArray.unshift(JSON.parse(addUser));
 		window.sessionStorage.setItem("addUser",JSON.stringify(addUserArray));
 		//弹出loading
 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            top.layer.close(index);
			top.layer.msg("用户添加成功！");
 			layer.closeAll("iframe");
	 		//刷新父页面
	 		parent.location.reload();
        },2000);
 		return false;*/
 	}) 
	
}) 
 

/*

$(function(){
            $("#inputForm").validate({
                submitHandler:function(form){
                    //验证通过后可提交表单
                    form.submit();
                    //ajax提交
                    $.ajax({
                        url:"www.onmore.cn",
                        type:'post',
                        beforeSend:function () {
                            //在表单提交后后端未作出响应时，禁用提交按钮，提高用户体验。
                            $("#submit").attr({ disabled: "disabled" });
                        },
                        complete:function(){
                            //后端响应后放开按钮
                            $("#submit").removeAttr("disabled");
                        },
                        success:function(data){
                            console.log(data);
                        }

                    });
                },
                rules:{
                    user:{
                        required:true,
                    },
                    address:{
                        required:true
                    }
                },
                messages:{
                    user:{
                        required:"请输入账号！",
                    },
                    address:{
                        required:"请输入地址"
                    }
                },
                onfocusout:false,//失去焦点时不执行验证
                errorPlacement:function(error,element){//错误提示，错误对象
                      layer.tips(error[0].innerText,element,{//1.错误信息，2提示位置，3同时提示多个错误
                        tipsMore:true//错误信息可以同时提示多个，...
                    }); 
                }
            });
        });


*/

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
